package com.xuanyuan.material

import android.graphics.Outline
import android.graphics.Path
import android.os.Bundle
import android.view.View
import android.view.ViewOutlineProvider
import com.google.android.material.shape.AbsoluteCornerSize
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.RelativeCornerSize
import com.google.android.material.shape.ShapeAppearanceModel
import com.xuanyuan.base.BaseActivity
import com.xuanyuan.material.databinding.ActivityShapeableImageviewBinding


/**
 * FileName: ShapeableImageView
 * @author ken.luo
 * Date: 2022/6/9 15:06
 * Description: 继承自ImageView，可以为image添加描边大小、颜色，以及圆角、裁切等，
 * History:
 * 创建者 : ken.luo
 * <author>     <time>     <version>      <desc>
 * 作者姓名      修改时间      版本号          描述
</desc></version></time></author> */

class ShapeableImageViewActivity : BaseActivity<ActivityShapeableImageviewBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setImage()
    }

    /**
     * 1.setTopLeft表示处理左上角，其他同理。
     * 2.cornerSize表示设置的大小，有RelativeCornerSize和AbsoluteCornerSize，RelativeCornerSize构造方法接收一个百分比，范围0-1；
     * AbsoluteCornerSize构造方法接收一个具体数值，这个数值就是圆角的数值。
     * 3.CornerFamily，它表示处理的方式，有ROUNDED和CUT两种，ROUNDED是圆角，CUT是直接将圆角部分裁切掉。
     * setAllCornerSizes(ShapeAppearanceModel.PILL)可以直接实现圆形效果。
     */
    private fun setImage() {
        mBinding.roundedImage.shapeAppearanceModel = ShapeAppearanceModel.builder()
            .setAllCorners(CornerFamily.ROUNDED, 60f)// 设置圆角
                .build()
        mBinding.cutImage.shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setTopLeftCorner(CornerFamily.CUT, RelativeCornerSize(0.4f))//设置指定方向圆角
                .setTopRightCorner(CornerFamily.CUT, RelativeCornerSize(0.4f))//设置指定方向圆角
                .setBottomRightCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))//设置指定方向圆角
                .setBottomLeftCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))//设置指定方向圆角
                .build()


        mBinding.randomImage.shapeAppearanceModel = ShapeAppearanceModel.builder()
            .setAllCornerSizes(ShapeAppearanceModel.PILL)
            .setTopLeftCornerSize(100f)//设置指定方向圆角大小
            .setTopRightCornerSize(RelativeCornerSize(0.5f))//设置指定方向圆角大小
            .setBottomLeftCornerSize(20f)//设置指定方向圆角大小
            .setBottomRightCornerSize(AbsoluteCornerSize(50f))//设置指定方向圆角大小
            .build()


        //绘制圆角
        mBinding.roundedImage1.apply {
            outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View, outline: Outline) {
                    outline.setRoundRect(0, 0, view.width, view.height, 80f)
                }
            }
            clipToOutline = true
        }
        //绘制圆形
        mBinding.cutImage2.apply {
            outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View, outline: Outline) {
                    outline.setOval(0, 0, view.width, view.width)
                }
            }
            clipToOutline = true
        }

        mBinding.randomImage2.apply {
            outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View, outline: Outline) {
                    view.elevation = 10F
                    val gap = 20F
                    val path = Path()
                    path.moveTo(-gap, -gap)
                    path.lineTo(-gap, view.measuredHeight.toFloat() + gap) //左下点
                    path.lineTo(
                        (view.measuredWidth / 2).toFloat(),
                        (view.measuredHeight + 50).toFloat()
                    )
                    path.lineTo(
                        view.measuredWidth.toFloat() + gap,
                        view.measuredHeight.toFloat() + gap
                    )
                    path.lineTo(view.measuredWidth.toFloat() + gap, -gap) //右上点
                    path.close()
                    outline.setConvexPath(path)
                    view.scaleX = 0.5F
                    view.scaleY = 0.5F
                }
            }
        }


    }

    override fun initLayout(): Int {
        return R.layout.activity_shapeable_imageview
    }

    override fun afterInit() {

    }


}


